package com.spm.librarianmanagersystem.controller;

import com.alibaba.fastjson.JSONObject;
import com.spm.librarianmanagersystem.mapper.MemberMapper;
import com.spm.librarianmanagersystem.pojo.Member;
import com.spm.librarianmanagersystem.service.LoginService;
import com.spm.librarianmanagersystem.utils.Code;
import com.spm.librarianmanagersystem.utils.JsonResult;
import com.spm.librarianmanagersystem.utils.TokenUtil;
import com.spm.librarianmanagersystem.utils.UserContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
public class LoginController {
    @Autowired
    private LoginService loginService;

    @Autowired
    private MemberMapper mm;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @PostMapping("/api/user/login")
    public JsonResult<String> userNormalLogin(@RequestBody JSONObject object){
        String token = loginService.userNormalLogin(object);
        if(token==null){
            return new JsonResult<>(null,1, "账号或密码错误");
        }
        return new JsonResult<>(token);
    }

    @PostMapping("/api/user/emaillogin")
    public JsonResult<String> userEmailLogin(@RequestBody JSONObject object){
        String token = loginService.userEmailLogin(object);
        if(token==null){
            return new JsonResult<>(null,1, "邮箱或验证码错误");
        }
        return new JsonResult<>(token);
    }

    //	获得发件人信息

    @PostMapping("/api/user/send")
    public JsonResult<String> save(@RequestBody JSONObject object) {
        String info = loginService.send(object);
        if(info==null){
            return new JsonResult<>(null,1, "邮箱错误或该邮箱未注册用户");
        }
        return new JsonResult<>(0, info);
    }

    @PostMapping("/api/user/login/check")
    public JsonResult<JSONObject> userTokenLogin(){
        String currentUserId = UserContextHolder.getCurrentUserId();
        System.out.println(currentUserId);
        return new JsonResult<>(loginService.userTokenLogin(currentUserId));
    }

    @PostMapping("/api/admin/login")
    public JsonResult<String> adminNormalLogin(@RequestBody JSONObject object){
        String token = loginService.adminNormalLogin(object);
        if(token==null){
            return new JsonResult<>(null,1, "账号或密码错误");
        }
        return new JsonResult<>(token);
    }

    @PostMapping("/api/admin/login/check")
    public JsonResult<JSONObject> adminTokenLogin(){
        String currentUserId = UserContextHolder.getCurrentUserId();
        System.out.println(currentUserId);
        return new JsonResult<>(loginService.adminTokenLogin(currentUserId));
    }
}
